#include <stddef.h>

#ifndef _AST_HEADER_
#define _AST_HEADER_
#include "ast.h"
#endif
#define bool int

#define true 1
#define false 0


enum STRUCT{
    TABLELIST,
    DATABASECONFIG,
    CREATESTRUCT,
    DELETESTRUCT,
    INSERTSTRUCT,
    INSERTFIELDSTRUCT,
    INSERTITEMSTRUCT,
    UPDATESTRUCT,
    UPDATEFIELDSTRUCT,
    SELECTSTRUCT,
    SELECTEDFIELDSTRUCT,
    SELECTEDTABLELIST,
    SELECTRESULTSTRUCT,
    CREATEFIELDSTRUCT,
    CONDITIONSTRUCT
};

enum EXECEUTERROR{
    CONFIG_FILE_OPEN_ERROR,
    CREATE_DATABASE_ERROR,
    DATABASE_NOT_EXIST_ERROR,
    CONFIG_TABLE_CREATE_ERROR,
    FILE_OPEN_ERROR,
    DIRECTORY_OPEN_ERROR};

enum SQLTYPE{
    HELPSQL, EXITSQL,USESQL,
    CREATEDATABASESQL, SHOWDATABASESSQL, 
    SHOWTABLESSQL,DESCSQL,
    DROPDATABASESQL,DROPTABLESQL,
    CREATESQL,
    SELECTSQL,INSERTSQL,UPDATESQL,DELETESQL
};


typedef struct DatabaseConfig{
    char * configPath;
    char * m_dataPath;
    char * m_instructmentPath;
    int tabLength;
}DatabaseConfigStruct, * DatabaseConfigStructPoint;


char * strpathcat(char * prefix,char * other);

//Get the database config;
void initDatabase();

void _exitDatabase();
void freeGlobalVariable();

//Error handle;
void handleError(enum EXECEUTERROR error,void * data);
//Read table list from database.
TableListStructPoint readTableList();
void freeStruct(enum STRUCT type,void * root);
//Write back table list.
bool writeBackTableList();

int calTabCnt(int max);
void borderPrint(int cnt);
void contentPrint(char * res,int cnt);

//Sql execution enter.
void executeSQL(void * syntaxTree,int type);


// help;
void help();
// exit;
//Create database dbname;
void createDatabase(char * dbname);
//Show database;
void showDatabases();

// drop database
void dropDatabase(char * dbname);
void useDatabase(char * dbname);
//Show tables;
void showTables();

void _select(SelectStructPoint  syntaxTree);

void insert(InsertStructPoint syntaxTree);

void update(UpdateStructPoint syntaxTree);

void _delete(DeleteStructPoint syntaxTree);

void dropTable(char * name);